#define IPC_MESSAGE_IMPL
#include "message.h"

// Generate constructors.
#include "ipc/struct_constructor_macros.h"
#include "message.h"

// Generate destructors.
#include "ipc/struct_destructor_macros.h"
#include "message.h"

namespace IPC {

// MSG_Request_Param
void ParamTraits<MSG_Request_Param>::Write(Message* m, const param_type& p) {
  p.request_url.WriteToPickle(m);
  p.save_path.WriteToPickle(m);
  m->WriteInt64(p.start_position);
}

bool ParamTraits<MSG_Request_Param>::Read(
    const Message* m, PickleIterator* iter, param_type* r) {
  if (!r->request_url.ReadFromPickle(iter)) return false;
  if (!r->save_path.ReadFromPickle(iter)) return false;
  if (!m->ReadInt64(iter, &r->start_position)) return false;
  return true;
}

void ParamTraits<MSG_Request_Param>::Log(const param_type& p, std::string* l) {
  l->append(StringPrintf("download:%ws error", p.request_url.LossyDisplayName()));
}

// MSG_Response_Param
void ParamTraits<MSG_Response_Param>::Write(Message* m, const param_type& p) {
  m->WriteInt(p.response_code);
}

bool ParamTraits<MSG_Response_Param>::Read(
    const Message* m, PickleIterator* iter, param_type* r) {
  if (!m->ReadInt(iter, &r->response_code)) return false;
  return true;
}

void ParamTraits<MSG_Response_Param>::Log(const param_type& p, std::string* l) {
  l->append(StringPrintf("response-error_code :%ws ", p.response_code));
}

// MSG_Progress_Param
void ParamTraits<MSG_Progress_Param>::Write(Message* m, const param_type& p) {
  m->WriteInt64(p.already_bytes);
  m->WriteInt64(p.total_bytes);
  m->WriteInt64(p.bytes_per_sec);
}

bool ParamTraits<MSG_Progress_Param>::Read(
    const Message* m, PickleIterator* iter, param_type* r) {
  if (!m->ReadInt64(iter, &r->already_bytes)) return false;
  if (!m->ReadInt64(iter, &r->total_bytes)) return false;
  if (!m->ReadInt64(iter, &r->bytes_per_sec)) return false;
  return true;
}

void ParamTraits<MSG_Progress_Param>::Log(const param_type& p, std::string* l) {
}

// MSG_Finished_Param
void ParamTraits<MSG_Finished_Param>::Write(Message* m, const param_type& p) {
  m->WriteInt(p.error_code);
  m->WriteWString(p.error_desc);
}

bool ParamTraits<MSG_Finished_Param>::Read(const Message* m, 
                                           PickleIterator* iter, param_type* r) {
  if (!m->ReadInt(iter, &r->error_code)) return false;
  if (!m->ReadWString(iter, &r->error_desc)) return false;
  return true;
}

void ParamTraits<MSG_Finished_Param>::Log(const param_type& p, std::string* l) {
}

}  // namespace IPC
